package org.example.base;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author HeZhuo
 * @date 2020-09-25
 */
@Schema(name = "返回信息")
@NoArgsConstructor
@AllArgsConstructor
@Data
public class R<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @Schema(description  = "状态码", requiredMode = Schema.RequiredMode.REQUIRED)
    private int code;

    @Schema(description = "是否成功", requiredMode = Schema.RequiredMode.REQUIRED)
    private boolean success;

    @Schema(description = "是否成功")
    private T data;

    @Schema(description = "返回消息", requiredMode = Schema.RequiredMode.REQUIRED)
    private String msg;

    private R(IResultCode resultCode) {
        this(resultCode, null, resultCode.getMessage());
    }

    private R(IResultCode resultCode, String msg) {
        this(resultCode, null, msg);
    }

    private R(IResultCode resultCode, T data) {
        this(resultCode, data, resultCode.getMessage());
    }

    private R(IResultCode resultCode, T data, String msg) {
        this(resultCode.getCode(), data, msg);
    }

    private R(int code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        this.success = ResultCode.SUCCESS.code == code;
    }

//    public static boolean isSuccess(@Nullable R<?> result) {
//        return Optional.ofNullable(result).map((x) ->
//                ObjectUtils.nullSafeEquals(ResultCode.SUCCESS.code, x.code)).orElse(Boolean.FALSE);
//    }
//
//    public static boolean isNotSuccess(@Nullable R<?> result) {
//        return !isSuccess(result);
//    }

    public static <T> R<T> data(T data) {
        return data(data, ResultCode.SUCCESS.getMessage());
    }

    public static <T> R<T> data(T data, String msg) {
        return data(ResultCode.SUCCESS.getCode(), data, msg);
    }

    public static <T> R<T> data(int code, T data, String msg) {
        return new R<>(code, data, data == null ? "暂无承载数据" : msg);
    }

    public static <T> R<T> success() {
        return success(ResultCode.SUCCESS);
    }

    public static <T> R<T> success(String msg) {
        return new R<>(ResultCode.SUCCESS, msg);
    }

    public static <T> R<T> success(IResultCode resultCode) {
        return new R<>(resultCode);
    }

    public static <T> R<T> success(IResultCode resultCode, String msg) {
        return new R<>(resultCode, msg);
    }

    /**
     * 返回一个失败信息，不被推荐使用，因为无法查看到失败原因
     */
    @Deprecated
    public static <T> R<T> fail() {
        return fail(ResultCode.FAILURE);
    }

    public static <T> R<T> fail(String msg) {
        return new R<>(ResultCode.FAILURE, msg);
    }

    public static <T> R<T> fail(int code, String msg) {
        return new R<>(code, null, msg);
    }

    public static <T> R<T> fail(IResultCode resultCode) {
        return new R<>(resultCode);
    }

    public static <T> R<T> fail(IResultCode resultCode, String msg) {
        return new R<>(resultCode, msg);
    }

    public static <T> R<T> status(boolean flag) {
        return flag ? success(ResultCode.SUCCESS.getMessage()) : fail("操作失败");
    }

    public static <T> R<T> status(Integer status) {
        return status(null != status && status >= 1);
    }

}